<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>27watch监听入门</title>
</head>
<body>
<div id="app">
    <input type="text" v-model="user">{{ user }}
    <br>
    <input type="text" v-model="age">{{ age }}
    <br>
    <input type="text" v-model="worker.age">{{ worker.age }}
</div>
<script src="../vue.global.3.0.5.js"></script>
<script>
    const outerAssembly = {
        data() {
            return {
                
            }
        },
        template: `
        <input type="text" value="Mr.GuoZi" ref="userZi" />
        `
    }

    //1、创建一个Vue应用实例，本身是最顶层的根组件
    const app = Vue.createApp({
        data() {
            return { 
                user: " Mr.Lee",
                age: 12,
                worker: {
                    age: 15
                }
            }
        },
        methods: {
            //方法名可以自定义
            getAge(newValue, oldValue) {
                console.log("newValue=" + newValue + ", oldValue=" + oldValue);
            }
        },
        computed: {
            //方法名可以自定义
            getWorkerAge() {
                return this.worker.age;
            }
        },
        watch: {
            //【监听写法1】方法名为监听的属性名
            user(newValue, oldValue) {
                console.log("newValue=" + newValue + ", oldValue=" + oldValue);
            },
            //【监听写法2】把监听内容封装到方法中
            age: "getAge",
            //【监听写法3】监听对象的属性时需要计算属性配合，被监听的属性为计算属性的名称
            getWorkerAge: "getAge"
        },
        components: {
            //命名规则必须是小写字母加-
            "outer-assembly": outerAssembly
        }
    });
    //2、定义一个全局组件
    app.component('button-counter', {
        //数据
        data() {
            return {
                counter: 100
            }
        },
        //模板
        template: `
            计算器: {{counter}}
            <button v-on:click="counter++">counter++</button><br>
        `
    });
    //3、挂载，返回值是代理对象并不是应用实例本身
    const vm = app.mount("#app");
</script>
</body>
</html>